// pages/my/store-entry/store-entry.js
const request = require('../../../../../utils/request')
const common = require('../../../../../utils/common')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //省市区
        region: [],
        regionString: '',

        area_id: '', //选择的省区id
        addressAll: null,


        // 倒计时
        timeText: '获取验证码',
        timeWait: 60,
        timeBack: 'rgba(238,126,48,1)',
        successCode: true,
         


    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        // this.getAreaList()
    },
    onShow() {
        // let storeAddress = wx.getStorageSync('storeAddress')
        // if(storeAddress){
        //     this.setData({
        //         address:storeAddress.address,
        //         lat:storeAddress.lat,
        //         lng:storeAddress.lng
        //     })
        // }
    },
    getAreaList() {
        let _this = this
        request._post('/api?method=util.getallarea', {}).then(res => {
            // console.log(res, '区域')
            _this.setData({
                addressAll: {
                    provinces: res.data.list,
                    citys: res.data.list1,
                    areas: res.data.list2
                }
            })
        })
    },
    // 子组件传值
    // sendData(e) {
    //     if (!e.detail.regionId) {
    //         return
    //     }
    //     this.setData({
    //         area_id: e.detail.regionId
    //     })
    // },

    inputWrite(e) {
        this.setData({
            [e.target.dataset.name]: e.detail.value
        })
    },
    // 选择详细地址
    selectAddress() {
        wx.navigateTo({
            url: '/pages/mine/pages/my/store-entry/store-entry_select',
        })
    },
    submit() {
        let {
            store_name,
            mobile,
            linkman,
            ver_code,
            // area_id,
            address
        } = this.data
        if (!linkman) {
            return getApp().toast('请输入真实姓名')
        }
        if (!this.data.mobile) {
            return getApp().toast('请输入手机号码')
        }
        if (!common.isMobile(this.data.mobile)) {
            return getApp().toast('请输入正确的手机号码')
        }

        if (!this.data.ver_code) {
            return getApp().toast('请输入手机验证码')
        }
        if (!this.data.store_name) {
            return getApp().toast('请输入门店名称')
        }
        // if (!this.data.area_id) {
        //     return getApp().toast('请选择门店地址')
        // }
        if (!this.data.address) {
            return getApp().toast('请选择详细地址')
        }
        request._get('/api?method=user.applybusiness', {
            store_name,
            mobile,
            linkman,
            ver_code,
            address,
            coordinate:this.data.lat + ',' + this.data.lng
        }).then(res => {
            wx.removeStorageSync('storeAddress')
            getApp().toast(res.msg)
            setTimeout(function(){
                wx.navigateBack({
                    delta: 1, 
                })
            },1000)
        })
    },
    onUnload() {
        wx.removeStorageSync('storeAddress')
    },
    /** 
     * 省市区
     */
    // bindRegionChange: function (e) {
    //     const region = e.detail.value;
    //     const regionString = region[0] + '-' + region[1] + '-' + region[2];
    //     this.setData({
    //         region: e.detail.value,
    //         regionString: regionString,
    //     })
    // },
    // 获取验证码
    getYzCode() {
        let _this = this
        // 判断是否填写手机号
        if (!this.data.mobile) {
            wx.showToast({
                icon: 'none',
                title: '请先填写手机号码',
                duration: 500
            })
        } else if (!common.isMobile(this.data.mobile)) {
            wx.showToast({
                icon: 'none',
                title: '请填写正确的手机号码格式',
                duration: 500
            })
        } else {
            // 开启倒计时
            if (!_this.data.successCode) {
                return
            }
            this.getCodeApi()
        }
    },
    // 获取验证码接口
    getCodeApi() {
        // 判定手机号是否被注册
        let _this = this
        let eventStr = ''
        request._get('/api?method=user.sms', {
            mobile: this.data.mobile,
            code: 'veri'
        },'noload').then(res => {
            wx.showToast({
                title: res.msg,
                icon: 'none'
            })
            _this.startTime()
        })
    },
    // 验证码倒计时
    startTime() {
        let _this = this
        var inter = setInterval(function () {
            _this.setData({
                timeWait: _this.data.timeWait - 1,
                timeText: _this.data.timeWait + 's',
                timeBack: 'rgba(245,245,245,1)',
            })
            if (_this.data.timeWait < 0) {
                clearInterval(inter)
                _this.setData({
                    timeWait: 60,
                    timeText: '获取',
                    timeBack: 'rgba(238,126,48,1)',
                    successCode: true
                })
            }
        }.bind(_this), 1000)
        if (_this.data.successCode) {
            _this.setData({
                successCode: false
            })
        }
    },


})